//
//  ZhiFuViewController.m
//  1243
//
//  Created by 我的 on 16/4/20.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "ZhiFuViewController.h"
#import "OrderPayTableViewCell.h"
#import "PayOrderTwoTableViewCell.h"
#import "SVProgressHUD.h"
#import "NetWorkTools.h"

@interface ZhiFuViewController ()
<
    UIAlertViewDelegate
>
{
    NSMutableArray *boolArray;
}

@property (nonatomic,copy)NSString *payType;
@property (nonatomic, copy) NSString *message;

@end

@implementation ZhiFuViewController

- (NSString *)payType {
    if (!_payType) {
        _payType = @"3";
    }
    return _payType;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    boolArray = [NSMutableArray arrayWithObjects:@"1",@"0", @"0", nil];
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"OrderPayTableViewCell" bundle:nil] forCellReuseIdentifier:@"OrderPayTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"PayOrderTwoTableViewCell" bundle:nil] forCellReuseIdentifier:@"PayOrderTwoTableViewCell"];
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return 2;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        OrderPayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderPayTableViewCell" forIndexPath:indexPath];
        cell.OrderPayPriceLabel.text = [NSString stringWithFormat:@"￥%.2f", self.allPrice];
        
        return cell;
    } else {
        
        PayOrderTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PayOrderTwoTableViewCell" forIndexPath:indexPath];
        
        cell.paySelectView.layer.borderWidth = 2;
        cell.paySelectView.layer.cornerRadius = CGRectGetHeight(cell.paySelectView.frame)/2;
        cell.paySelectView.layer.masksToBounds = YES;
        
        if (indexPath.row == 0) {
            cell.PayTwoTitleLabel.text = @"广告积分";
        } else {
            cell.PayTwoTitleLabel.text = @"广告积分20%+广告分红积分80%";
        }
        
        if ([boolArray[indexPath.row] isEqualToString:@"1"]) {
            cell.paySelectView.backgroundColor = [UIColor greenColor];
        } else {
            cell.paySelectView.backgroundColor = [UIColor whiteColor];
        }
        
        return cell;
    }
}

#pragma mark - UITableView Delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 1) {
        
        for (NSInteger i=0; i<boolArray.count; i++) {
            if (indexPath.row == i) {
                boolArray[i] = @"1";
            }else{
                boolArray[i] = @"0";
            }
        }
        
        if (indexPath.row == 0) {
            self.payType = @"3";
        } else {
            self.payType = @"5";
        }
    }
    
    NSLog(@"%@",self.payType);
    
    [self.tableView reloadData];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    if (section ==  1) {
        return @"支付方式";
    } else {
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 0) {
        return 0.01;
    } else {
        return 60;
    }
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 1) {
        UIView *footView = [[UIView alloc] init];
        UIButton *GoPayMentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        GoPayMentBtn.frame = CGRectMake(30, 10, WEITH-60, 40);
        GoPayMentBtn.backgroundColor = [UIColor colorWithRed:52/255. green:147/255. blue:225/255. alpha:1];
        [GoPayMentBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        GoPayMentBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        GoPayMentBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [GoPayMentBtn setTitle:@"立即支付" forState:UIControlStateNormal];
        [GoPayMentBtn addTarget:self action:@selector(GoPayMentBtnClick) forControlEvents:UIControlEventTouchUpInside];
        GoPayMentBtn.layer.cornerRadius = 6;
        GoPayMentBtn.layer.masksToBounds = YES;
        [footView addSubview:GoPayMentBtn];
        return footView;
    }
    return nil;
}

- (void)GoPayMentBtnClick {
    
//    [SVProgressHUD show];
//    
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        
//        [SVProgressHUD dismiss];
//        
//        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"支付失败" message:@"账户余额不足" preferredStyle:UIAlertControllerStyleAlert];
//        
//        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//            
//        }];
//        
//        [alert addAction:action];
//        
//        [self presentViewController:alert animated:YES completion:nil];
//    });

    NSLog(@"%@",self.orderArray);
    
    __block int num = 0;
    for (int i = 0; i < self.orderArray.count; i++) {
        
        NSDictionary *dict = [ReadLoginData readLoginData];
        
        NSString *userId = [dict objectForKey:@"Number"];
        
        NSDictionary *params = @{
                                 @"UserId":userId,
                                 @"UserName":[dict objectForKey:@"UserName"],
                                 @"OrderNumber":self.orderArray[i],
                                 @"PayMoneyType":self.payType
                                 };
        
        [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"PAYORDER" completeBlock:^(id object) {
            NSLog(@"%@",object)
            
            self.message = [object objectForKey:@"Message"];
            if ([object[@"Id"] integerValue] == 0) {
                NSMutableArray *array = [NSMutableArray array];
                [array addObjectsFromArray:[ReadLoginData readShopArr]];
                for (int i = 0; i < array.count; i ++) {
                    if ([array[i][@"isPay"] integerValue] == 1) {
                        [array removeObjectAtIndex:i];
                        [ReadLoginData saveShopDataToLocal:array];
                    }
                }
                
            } else {
                num = num + 1;
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:self.message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                alert.tag = 22222;
                [alert show];
            }
            
            if (i == self.orderArray.count - 1) {
                if (num == 0) {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:self.message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    alert.tag = 11111;
                    [alert show];
                }
            }
            
            
        }];

        
    }
    
    
    
}

/**
 *  删除购物车订单
 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 11111) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}


@end
